package org.example.admin.manage.repository;

import org.example.commons.entity.SysAdmin;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * author 陈伯瑞
 * date 2020/5/25 0025 17:35
 * version 1.0
 **/
@Repository
public interface SysAdminRepository extends JpaRepository<SysAdmin, Long> {

    SysAdmin findByOpenid(String openid);
    SysAdmin findByPhone(String phone);
    SysAdmin findByPhoneAndStatus(String phone, Integer status);
    SysAdmin findByName(String name);
    void deleteByAdminIdIn(Long[] ids);

    List<SysAdmin> findByAdminIdIn(Long[] ids);

    @Query("select admin.password from SysAdmin admin " +
            "where admin.adminId = :id")
    String getPassword(Long id);

    @Modifying
    @Transactional
    @Query("update SysAdmin admin set admin.password = :password " +
            "where admin.adminId = :id")
    void setPassword(Long id, String password);

    /**
     * 条件查询
     * @return
     */
    @Query(value = "select admin from SysAdmin admin " +
            "where admin.name like :name  " +
            " and admin.phone like :phone " +
            " and (:status IS NULL or admin.status = :status ) " +
            " and ( :#{#dates[0]} IS NULL OR :#{#dates[1]} IS NULL " +
            " OR admin.createTime between :#{#dates[0]} and :#{#dates[1]})" )
    Page<SysAdmin> searchList(String name, String phone,Integer status, Date[] dates, Pageable pageable);

}
